/* Copyright(c)2012, Dai Yucheng.
* All rights reserved.
* daiycheng@gmail.com.
*/


#include "StdAfx.h"
#include "CvvImage.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CV_INLINE RECT NormalizeRect( RECT r );
CV_INLINE RECT NormalizeRect( RECT r )
{
	int t;

	if( r.left > r.right )
	{
		t = r.left;
		r.left = r.right;
		r.right = t;
	}

	if( r.top > r.bottom )
	{
		t = r.top;
		r.top = r.bottom;
		r.bottom = t;
	}

	return r;
}

CV_INLINE CvRect RectToCvRect( RECT sr );
CV_INLINE CvRect RectToCvRect( RECT sr )
{
	sr = NormalizeRect( sr );
	return cvRect( sr.left, sr.top, sr.right - sr.left, sr.bottom - sr.top );
}

CV_INLINE RECT CvRectToRect( CvRect sr );
CV_INLINE RECT CvRectToRect( CvRect sr )
{
	RECT dr;
	dr.left = sr.x;
	dr.top = sr.y;
	dr.right = sr.x + sr.width;
	dr.bottom = sr.y + sr.height;

	return dr;
}

CV_INLINE IplROI RectToROI( RECT r );
CV_INLINE IplROI RectToROI( RECT r )
{
	IplROI roi;
	r = NormalizeRect( r );
	roi.xOffset = r.left;
	roi.yOffset = r.top;
	roi.width = r.right - r.left;
	roi.height = r.bottom - r.top;
	roi.coi = 0;

	return roi;
}

void  FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin )
{
	assert( bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));

	BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);

	memset( bmih, 0, sizeof(*bmih));
	bmih->biSize = sizeof(BITMAPINFOHEADER);
	bmih->biWidth = width;
	bmih->biHeight = origin ? abs(height) : -abs(height);
	bmih->biPlanes = 1;
	bmih->biBitCount = (unsigned short)bpp;
	bmih->biCompression = BI_RGB;

	if( bpp == 8 )
	{
		RGBQUAD* palette = bmi->bmiColors;
		int i;
		for( i = 0; i < 256; i++ )
		{
			palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
			palette[i].rgbReserved = 0;
		}
	}
}

CvvImage::CvvImage()
{
}

CvvImage::~CvvImage()
{
}



int CvvImage::Bpp()
{ 
	switch (m_image.depth())
	{
	case 0:
	case 1:
		return 8 * m_image.channels();
	case 2:
	case 3:
		return 16 * m_image.channels();
	case 4:
	case 5:
		return 32 * m_image.channels();
	case 6:
		return 64 * m_image.channels();
	}

	return 0;
};

bool  CvvImage::Load( const char* filename, int desired_color )
{
	m_image = cv::imread(filename, desired_color);
	if ( m_image.empty())
		return false;

	AlignByte();

	return true;
}

void CvvImage::AlignByte()
{
	switch (m_image.channels())
	{
	case 1:
		cv::cvtColor(m_image, m_image, CV_GRAY2RGBA);
		break;
	case 3:
		cv::cvtColor(m_image, m_image, CV_RGB2RGBA); 
		break;
	case 4:
		break;
	default:
		return;
	}
}

int CvvImage::Width()  
{
	if (m_image.empty())
		return 0;

	cv::Size roi_locate;
	cv::Point ofs;
	m_image.locateROI(roi_locate,ofs);

	return roi_locate.width;
}

int CvvImage::Height()
{
	if (m_image.empty())
		return 0;

	cv::Size roi_locate;
	cv::Point ofs;
	m_image.locateROI(roi_locate,ofs);

	return roi_locate.height;
}

void CvvImage::GetROI(cv::Mat& roiMat, CvRect& r)
{
	assert(r.x >=0 && 
		r.y >=0 && 
		r.width <= m_image.cols && 
		r.height <= m_image.rows);

	roiMat = m_image(r);
}

bool  CvvImage::LoadRect( const char* filename,	int desired_color, CvRect r )
{
	if( r.width < 0 || r.height < 0 ) 
		return false;

	cv::Mat image = cv::imread(filename, desired_color);
	if (image.empty())
		return false;

	if( r.width == 0 || r.height == 0 )
	{
		r.width = image.cols;
		r.height = image.rows;
		r.x = r.y = 0;
	}

	if( r.x > image.cols || r.y > image.rows ||
		r.x + r.width < 0 || r.y + r.height < 0 )
	{
		image.release();
		return false;
	}

	/* truncate r to source image */
	if( r.x < 0 )
	{
		r.width += r.x;
		r.x = 0;
	}
	if( r.y < 0 )
	{
		r.height += r.y;
		r.y = 0;
	}

	if( r.x + r.width > image.cols )
		r.width = image.cols - r.x;

	if( r.y + r.height > image.rows )
		r.height = image.rows - r.y;

	image = image(r);
	image.copyTo(m_image);
	image.release();

	AlignByte();
	return true;
}

bool  CvvImage::Save( const char* filename )
{
	if(m_image.empty())
		return false;

	cv::imwrite(filename, m_image);

	return true;
}

void  CvvImage::Show( const char* window )
{
	if (m_image.empty())
		return;

	cv::imshow(window, m_image);
}

void  CvvImage::Show( HDC dc, int x, int y, int w, int h, int from_x, int from_y )
{
	if (!m_image.empty() &&( m_image.depth() == 0 || m_image.depth()==1)) // IPL_DEPTH_8U IPL_DEPTH_8S
	{
		// Need to erase the region first and then modify it to reflect the
		// region we are about to actually paint.
		BitBlt(dc, 0, 0, w, h, NULL, 0, 0, BLACKNESS);
		
		uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
		BITMAPINFO* bmi = (BITMAPINFO*)buffer;
		int bmp_w = m_image.cols, bmp_h = m_image.rows;
		
		FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), 0 );
		
		from_x = MIN( MAX( from_x, 0 ), bmp_w - 1 );
		from_y = MIN( MAX( from_y, 0 ), bmp_h - 1 );
		
		int sw = MAX( MIN( bmp_w - from_x, w ), 0 );
		int sh = MAX( MIN( bmp_h - from_y, h ), 0 );
		SetDIBitsToDevice(
			dc, x, y, bmp_w, bmp_h, from_x, from_y, from_y, bmp_h,
			m_image.data + from_y * m_image.cols,
			bmi, DIB_RGB_COLORS );
	}
}

void  CvvImage::DrawToHDC( HDC hDCDst, RECT* pDstRect ) 
{
	if(!m_image.empty() &&( m_image.depth() == 0 || m_image.depth()==1))
	{
		uchar buffer[sizeof(BITMAPINFOHEADER) + 1024];
		BITMAPINFO* bmi = (BITMAPINFO*)buffer;
		int bmp_w = m_image.cols, bmp_h = m_image.rows;

		cv::Size roi;
		cv::Point ofs;
		m_image.locateROI(roi,ofs);

		CvRect dst = RectToCvRect( *pDstRect );

		if( roi.width == dst.width && roi.height == dst.height )
		{
			Show( hDCDst, dst.x, dst.y, dst.width, dst.height, ofs.x, ofs.y );
			return;
		}

		if( roi.width > dst.width )
		{
			SetStretchBltMode( hDCDst,HALFTONE );
		}
		else
		{
			SetStretchBltMode(hDCDst, COLORONCOLOR );
		}

		FillBitmapInfo( bmi, bmp_w, bmp_h, Bpp(), 0 );

		::StretchDIBits(
			hDCDst,
			dst.x, dst.y, dst.width, dst.height,
			ofs.x, ofs.y, m_image.cols, roi.height,
			m_image.data, 
			bmi, DIB_RGB_COLORS, SRCCOPY );
	}
}

void  CvvImage::Fill( int elemValue )
{
	m_image.setTo( cv::Scalar(elemValue&255,(elemValue>>8)&255,
		(elemValue>>16)&255,
		(elemValue>>24)&255) );
}